package mobilegeniusapp;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;

public class Midlet extends MIDlet implements CommandListener {

    private Form menu;
    private Command sair, jogar, voltar;
    private Display dsp;
    private Displayable curr;
    private Image image;
    private Vector cor;
    private int nivel = 0;
    private int pontos = 0;
    private TextField nome;
    private Timer timer;
    private String keyText = "";

    public Midlet() {
        cor = new Vector();
        menu = new Form("Mobile Genius Java ME 1.3");
        jogar = new Command("JOGAR", Command.OK, 0);
        sair = new Command("SAIR", Command.OK, 1);
        try {
            image = Image.createImage("/mobilegeniusapp/logo.jpg");
        } catch (IOException ex) {
        }
        nome = new TextField("Digite seu nome:", "", 30, TextField.INITIAL_CAPS_WORD);
        menu.append(image);
        menu.append(nome);
        menu.addCommand(jogar);
        menu.addCommand(sair);
        menu.setCommandListener(this);
    }

    public void startApp() {
        dsp = Display.getDisplay(this);
        dsp.setCurrent(menu);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == jogar) {
            exibirR();
        }

        if (c == voltar) {
            dsp.setCurrent(menu);
        }
    }

    public void exibirR() {
        curr = new Midlet.NumerosCanvas();
        dsp.setCurrent(curr);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            int conta = 0;
            int n = Midlet.this.nivel;
            public void run() {
                dsp.setCurrent(new SplashScreen(dsp, curr, random()));
                conta = conta + 1;
                if (conta >= Midlet.this.nivel) {
                    timer.cancel();
                }
            }
        }, 1000, 1300);
    }

    public void exibirB(int b) {
        final int button = b;
        curr = new Midlet.NumerosCanvas();
        dsp.setCurrent(curr);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                dsp.setCurrent(new SplashScreen(dsp, curr, button));
            }
        }, 100);
    }

    private int random() {
        Random sorteio = new Random();
        int k = sorteio.nextInt(9) + 1;
        armazena(k);
        return k;
    }

    private void armazena(int c) {
        cor.addElement(new Integer(c));
    }

    /**
     * public int random() { Random sorteio = new Random(); int k =
     * sorteio.nextInt(9) + 1; cor = new int[nivel+1]; cor[nivel] = k; //
     * mensagem.setText(String.valueOf(cor[nivel - 1])); return k; } *
     */
    public String verifica(int b) {
        int button = b;
        if ((Integer.parseInt(cor.lastElement().toString()) == button)) {
//            dsp.setCurrent(new SplashScreen(dsp, curr, button));
//            dsp.setCurrent(curr);
            keyText = "Parabens, você acertou !!!";
            pontos = pontos + 1;
            nivel = nivel + 1;
        } else {
            keyText = "VOCÊ ERROU !!!";
            nivel = 1;
            pontos = 0;
        }
        return keyText;
    }

    public class SplashScreen extends Canvas {

        int n = 0;
        private Display display;
        private Displayable next;
        private Timer timer = new Timer();

//o construtor da classe recebe um objeto display (tela atual)
//e um objeto Displayable que é a próxima tela, o midlet
        public SplashScreen(Display display, Displayable next, int b) {
            n = b;
            this.display = display;
            this.next = next;
            display.setCurrent(this);
            setFullScreenMode(true);
        }

        //método responsável por carregar uma imagem e printar na tela de splash
        protected void paint(Graphics g) {

            if (n == 1) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/1.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 2) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/2.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 3) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/3.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 4) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/4.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 5) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/5.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 6) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/6.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 7) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/7.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 8) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/8.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 9) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/9.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    Manager.playTone(n + 90, 1000, 100);
                } catch (IOException ex) {
                } catch (MediaException ex) {
                }
            }

            if (n == 15) {
                try {
                    g.drawImage(Image.createImage("/mobilegeniusapp/modelo_sorteio.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                    g.setColor(100, 100, 100);
                    g.drawString(cor.elementAt(nivel) + " - " + String.valueOf(pontos), 50, 300, Graphics.BASELINE | Graphics.HCENTER);
                } catch (IOException ex) {
                }
            }
        }

        protected void showNotify() {
            int tempo = 1000; //tempo de exibição da tela splash em milisegundos
            timer.schedule(new CountDown(), tempo);
        }

        private void dismiss() {
            timer.cancel();
            display.setCurrent(next);
        }

//encerra o run() quando acaba o tempo
        private class CountDown extends TimerTask {

            public void run() {
                dismiss();
            }
        }
    }

    class ExibirTimerTask extends TimerTask {

        public void run() {
            exibirR();
            timer.cancel();
        }
    }

    class NumerosCanvas extends Canvas implements CommandListener, Runnable {

        protected void paint(Graphics g) {
            try {
                g.drawImage(Image.createImage("/mobilegeniusapp/modelo_sorteio.png"), getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                g.setColor(255, 255, 255);
                g.drawString(keyText + " " + String.valueOf(pontos), 100, 300, Graphics.BASELINE | Graphics.HCENTER);
            } catch (IOException ex) {
            }
        }

        protected void keyPressed(int keyCode) {
//            exibirB(Integer.parseInt(getKeyName(keyCode)));
            verifica(Integer.parseInt(getKeyName(keyCode)));
            exibirR();
            //            dsp.setCurrent(new SplashScreen(dsp, curr, Integer.parseInt(getKeyName(keyCode))));
//            dsp.setCurrent(curr);
//            repaint();

        }

        public void commandAction(Command c, Displayable d) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        public void run() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
}